package com.cennavi.minemapdemo.activity.overlayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.activity.BaseActivty;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.DensityUtil;
import com.cennavi.minemapdemo.util.RandomUtil;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.camera.CameraUpdateFactory;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minedata.minemap.overlay.Marker;
import com.minedata.minemap.overlay.MarkerOptions;

import java.util.ArrayList;
import java.util.List;

public class MarkerActivity extends BaseActivty {

    //地图视图
    private MapView mapView;
    //地图操作类
    private MineMap mineMap;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        MinemapAccountManager.getInstance(this, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);
        setContentView(R.layout.activity_mark);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        //初始化地图
        initMap();
    }

    public void zoomIn(View view){
        if (mineMap != null){
            mineMap.moveCamera(CameraUpdateFactory.zoomIn());
        }
    }

    public void zoomOut(View view){
        if (mineMap != null){
            mineMap.moveCamera(CameraUpdateFactory.zoomOut());
        }
    }

    private void initMap() {

        //mine map sdk 2.0.6之前生效
        // mapView.setStyleUrl(Config.mBaseUrl + Config.MAP_ID);
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap oMineMap) {
                mineMap = oMineMap;
                mineMap.getUiSettings().setZoomControlsEnabled(false);
                mineMap.getUiSettings().setRotateGesturesEnabled(false);// 是否可以旋转地图
                mineMap.getUiSettings().setTiltGesturesEnabled(false);
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {
                    @Override
                    public void onStyleLoaded(String s) {

                        //mine map sdk 2.0.6及之后版本这样使用
                        //layers 和 images 都在这里面加载
                        addImageToMap();
                    }
                });
                //设置指南针是否显示，默认是显示
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.getUiSettings().setCompassGravity(Gravity.LEFT);
//                mineMap.getUiSettings().setCompassImage(getResources().getDrawable(R.mipmap.icon_compass));
                mineMap.getUiSettings().setCompassMargins(DensityUtil.dip2px(mContext, 11), DensityUtil.dip2px(mContext, 80),
                        0, 0);
                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(new LatLng(Config.mapCentrelat,Config.mapCentrelon)).zoom(14)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                mineMap.setMaxZoomPreference(17);//设置最高展示级别，最高为17级别
                mineMap.setMinZoomPreference(3);//设置最低的展示级别

                mineMap.setInfoWindowAdapter(new MineMap.InfoWindowAdapter() {
                    @Nullable
                    @Override
                    public View getInfoWindow(@NonNull Marker marker) {
                        View view = LayoutInflater.from(mContext).inflate(R.layout.marker_windows, mapView, false);
                        TextView name = view.findViewById(R.id.name);
                        name.setText(marker.getTitle());
                        TextView address = view.findViewById(R.id.address);
                        address.setText(marker.getSnippet());
                        return view;
                    }
                });
                mineMap.setOnMarkerClickListener(new MineMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(@NonNull Marker marker) {
                        snackbar(mapView,marker.toString());
                        return false;
                    }
                });
                mineMap.setOnInfoWindowClickListener(new MineMap.OnInfoWindowClickListener() {
                    @Override
                    public boolean onInfoWindowClick(@NonNull Marker marker) {
                        snackbar(mapView,marker.toString());
                        return false;
                    }
                });
            }

        });
    }

    //加载可能的图片资源
    private void addImageToMap() {

        icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher22);
        mineMap.addImage(R.mipmap.ic_launcher22+"", icon);

        icon1 = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.event_rtic_code1000_small);
        mineMap.addImage(R.mipmap.event_rtic_code1000_small+"", icon1);

    }


    private List<Marker> markerList;// MarkerLayer
    private Bitmap icon = null, icon1;


    /**
     * 加点
     */
    public void addmarklayer(View view) {
        // TODO: 2018/11/6 测试一下这个
        mineMap.setMultipleInfoWindows(true);
        if (markerList == null) {
            markerList = new ArrayList<>();
        }
        //绘制marklayer
        if (icon == null) {
            icon = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher22);
        }
        LatLng latLng = RandomUtil.randomLatLon();
        MarkerOptions markerOptions = new MarkerOptions().icon(R.mipmap.ic_launcher22+"",icon).title(System.currentTimeMillis()+"").snippet("hello").position(latLng);
        Marker marker = mineMap.addMarker(markerOptions);
//        mineMap.selectMarker(marker);

        markerList.add(marker);
    }

    /**
     * 移动
     * @param view
     */
    public void onMove(View view) {
        for ( Marker marker : markerList) {
            marker.hideInfoWindow();
            marker.setPosition(new LatLng((marker.getPosition().getLatitude() - 0.001), marker.getPosition().getLongitude()));
        }
    }

    /**
     * 改图标
     * @param view
     */
    public void checkIcon(View view) {

        icon = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.event_rtic_code1000_small);

        for ( Marker marker : markerList) {
            marker.setIcon("icon2", icon);
        }

    }

    /**
     * 清除
     * @param view
     */
    public void onRemove(View view) {
//        LatLng latLng = RandomUtil.randomLatLon();
//        CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng)
//                .build();
//        mineMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 1000);
        for ( Marker marker : markerList) {
            mineMap.removeMarker(marker);
//            marker.setPosition(new LatLng((marker.getPosition().getLatitude() - 0.001), marker.getPosition().getLongitude()));
        }
    }


    @Override
    protected void onResume() {
        if (mapView != null){
            mapView.onResume();
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mapView != null){
            mapView.onPause();
        }
        super.onPause();

    }
    @Override
    protected void onStart() {
        if (mapView != null) {
            mapView.onStart();
        }
        super.onStart();
    }

    @Override
    public void onStop() {
        if (mapView != null)
            mapView.onStop();
        super.onStop();
    }
    @Override
    public void onDestroy() {

        //2.0.6及之后版本 需要将加入地图的图片资源进行清理
        if (mineMap != null)
            mineMap.removeImagesAll();

        if (mapView != null) {
            mapView.onDestroy();
        }

        super.onDestroy();
    }

}
